﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.UserControls.StatusOverview
{
    public partial class ucServiceDemandOverview : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                BindServiceDemandData(db);
            }
        }

        /// <summary>
        /// Bind chart data
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindServiceDemandData(TireServiceDB db)
        {
            var servicedemand = from p in db.Cars
                                join v in db.TireSets on p.CarId equals v.CarId into zv
                                from x in zv.DefaultIfEmpty()
                                join b in db.ContainerCargoRooms on x.ContainerCargoRoomId equals b.ContainerCargoRoomId into xb
                                from n in xb.DefaultIfEmpty()
                                join a in db.CarStatus on p.CarStatusId equals a.CarStatusId into pa
                                from c in pa.DefaultIfEmpty()
                                join o in db.Containers on n.ContainerId equals o.ContainerId into no
                                from s in no.DefaultIfEmpty()
                                group n by new
                                {
                                    s.ContainerNumber,
                                    CarStatusDescription = c.Description
                                }
                                    into grp
                                    select new
                                    {
                                        grp.Key.ContainerNumber,
                                        grp.Key.CarStatusDescription,
                                        CarStatusCount = grp.Count()
                                    };

            chartServiceDemand.SeriesDataMember = "ContainerNumber";
            chartServiceDemand.SeriesTemplate.ArgumentDataMember = "CarStatusDescription";
            //chartServiceDemand.SeriesTemplate.Label.TextOrientation = DevExpress.XtraCharts.TextOrientation.BottomToTop;
            chartServiceDemand.SeriesTemplate.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
            chartServiceDemand.SeriesTemplate.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
            chartServiceDemand.SeriesTemplate.ValueDataMembers[0] = "CarStatusCount";

            chartServiceDemand.DataSource = servicedemand;
            chartServiceDemand.DataBind();            
        }
    }
}